using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace RLFtrClc
{
    public class Fighters : List<Fighter>
    {

        public Fighters()
        {

        }

        #region Create Armor Functions

        public void CreateArmor(string FighterType, ICalculatedFighter CalculatedFighter)
        {
            createArmor(FighterType, CalculatedFighter);
        }

        private void createArmor(string fighterType, ICalculatedFighter calculatedFighter)
        {
            double armorSum = 0;
            foreach (Fighter fighter in this)
            {
                armorSum += (double)fighter.Armor;
            }
            Double armorValue = (armorSum / 100);

            switch (fighterType)
            {
                case "Squadron":
                    calculatedFighter.setArmorRow(1, Convert.ToInt32(Math.Round(armorValue)));
                    break;
                case "Flight":
                    setFlightArmorLabels(armorValue, calculatedFighter);
                    break;
                case "Group":
                    setGroupArmorLabels(armorValue, calculatedFighter);
                    break;
            }
        }

        private void setGroupArmorLabels(Double armorValue, ICalculatedFighter calculatedFighter)
        {
            if (armorValue == 72)
            {
                calculatedFighter.setArmorRow(1, 6);
                calculatedFighter.setArmorRow(2, 8);
                calculatedFighter.setArmorRow(3, 10);
                calculatedFighter.setArmorRow(4, 13);
                calculatedFighter.setArmorRow(5, 16);
                calculatedFighter.setArmorRow(6, 19);
            }
            else
            {
                int armorRows = Convert.ToInt32(Math.Round(armorValue));
                int blocks = 72 - armorRows;
               
                while(blocks > 0)
                {
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(6, calculatedFighter.getArmorRow(6) - 1);
                        blocks--;
                    }
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(5, calculatedFighter.getArmorRow(5) - 1);
                        blocks--;
                    }
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(4, calculatedFighter.getArmorRow(4) - 1);
                        blocks--;
                    }
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(3, calculatedFighter.getArmorRow(3) - 1);
                        blocks--;
                    }
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(2, calculatedFighter.getArmorRow(2) - 1);
                        blocks--;
                    }
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(1, calculatedFighter.getArmorRow(1) - 1);
                        blocks--;
                    }
                }
            }
        }

        private void setFlightArmorLabels(Double armorValue, ICalculatedFighter calculatedFighter)
        {
            if (armorValue == 24)
            {
                calculatedFighter.setArmorRow(1, 6);
                calculatedFighter.setArmorRow(2, 8);
                calculatedFighter.setArmorRow(3, 10);
            }
            else
            {
                int armorRows = Convert.ToInt32(Math.Round(armorValue));
                int blocks = 24 - armorRows;

                while(blocks > 0)
                {
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(3, calculatedFighter.getArmorRow(3) - 1);
                        blocks--;
                    }
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(2, calculatedFighter.getArmorRow(2) - 1);
                        blocks--;
                    }
                    if (blocks > 0)
                    {
                        calculatedFighter.setArmorRow(1, calculatedFighter.getArmorRow(1) - 1);
                        blocks--;
                    }
                }
            }
        }

        #endregion

        #region Create LRA Functions

        public void CreateLRA(string FighterType, ICalculatedFighter CalculatedFighter)
        {
            createLRA(FighterType, CalculatedFighter);
        }

        private void createLRA(string fighterType, ICalculatedFighter calculatedFighter)
        {
            double LRASum = 0;
            foreach (Fighter fighter in this)
            {
                LRASum += (double)fighter.LRA;
            }
            Double LRAValue = (LRASum / 20);

            switch (fighterType)
            {
                case "Squadron":
                    calculatedFighter.setLRARow(1, Convert.ToInt32(Math.Round(LRAValue)));
                    break;
                case "Flight":
                    setFlightLRALabels(LRAValue, calculatedFighter);
                    break;
                case "Group":
                    setGroupLRALabels(LRAValue, calculatedFighter);
                    break;
            }
        }

        private void setGroupLRALabels(Double LRAValue, ICalculatedFighter calculatedFighter)
        {
            int LRARows = Convert.ToInt32(Math.Round(LRAValue));
            int remainder = 0;
            double eachRow = (LRARows / 6);
            eachRow = Math.Round(eachRow);

            calculatedFighter.setLRARow(6, LRARows);
            calculatedFighter.setLRARow(5, (LRARows - (int)eachRow));
            calculatedFighter.setLRARow(4, (LRARows - (2 * (int)eachRow)));
            calculatedFighter.setLRARow(3, (LRARows - (3 * (int)eachRow)));
            calculatedFighter.setLRARow(2, (LRARows - (4 * (int)eachRow)));
            calculatedFighter.setLRARow(1, (int)eachRow);
        }

        private void setFlightLRALabels(Double LRAValue, ICalculatedFighter calculatedFighter)
        {
            int LRARows = Convert.ToInt32(Math.Round(LRAValue));
            int remainder = 0;
            double eachRow = (LRARows / 3.0);
            eachRow = Math.Round(eachRow);

            calculatedFighter.setLRARow(3, LRARows);
            calculatedFighter.setLRARow(2, (LRARows - (int)eachRow));
            calculatedFighter.setLRARow(1, (int)eachRow);
        }
        #endregion

        #region Create CRA Functions

        public void CreateCRA(string FighterType, ICalculatedFighter CalculatedFighter)
        {
            createCRA(FighterType, CalculatedFighter);
        }

        private void createCRA(string fighterType, ICalculatedFighter calculatedFighter)
        {
            double cRASum = 0;
            foreach (Fighter fighter in this)
            {
                cRASum += (double)fighter.CRA;
            }
            Double cRAValue = (cRASum / 20);

            switch (fighterType)
            {
                case "Squadron":
                    calculatedFighter.setCRARow(1, Convert.ToInt32(Math.Round(cRAValue)));
                    break;
                case "Flight":
                    setFlightCRALabels(cRAValue, calculatedFighter);
                    break;
                case "Group":
                    setGroupCRALabels(cRAValue, calculatedFighter);
                    break;
            }
        }

        private void setGroupCRALabels(Double CRAValue, ICalculatedFighter calculatedFighter)
        {
            int CRARows = Convert.ToInt32(CRAValue);
            double eachRow = (CRARows / 6.0);
            eachRow = Math.Round(eachRow);

            calculatedFighter.setCRARow(6, CRARows);
            calculatedFighter.setCRARow(5, (CRARows - (int)eachRow));
            calculatedFighter.setCRARow(4, (CRARows - (2 * (int)eachRow)));
            calculatedFighter.setCRARow(3, (CRARows - (3 * (int)eachRow)));
            calculatedFighter.setCRARow(2, (CRARows - (4 * (int)eachRow)));
            calculatedFighter.setCRARow(1, (int)eachRow);
        }

        private void setFlightCRALabels(Double CRAValue, ICalculatedFighter calculatedFighter)
        {
            int CRARows = Convert.ToInt32(Math.Round(CRAValue));
            double eachRow = (CRARows / 3.0);
            eachRow = Math.Round(eachRow);

            calculatedFighter.setCRARow(3, CRARows);
            calculatedFighter.setCRARow(2, (CRARows - (int)eachRow));
            calculatedFighter.setCRARow(1, (int)eachRow);
        }
        #endregion

        #region Create Thrust Functions

        private int findLowestThrust(Fighter x, Fighter y)
        {
            return x.Thrust.CompareTo(y.Thrust);
        }

        public void CreateThrust(string FighterType, ICalculatedFighter CalculatedFighter)
        {
            createThrust(FighterType, CalculatedFighter);
        }

        private void createThrust(string fighterType, ICalculatedFighter calculatedFighter)
        {
            int thrust = 0;
            this.Sort(findLowestThrust);
            thrust = this[0].Thrust;

            switch (fighterType)
            {
                case "Squadron":
                    calculatedFighter.setMovement(thrust + 4);
                    break;
                case "Flight":
                    calculatedFighter.setMovement(thrust + 2);
                    break;
                case "Group":
                    calculatedFighter.setMovement(thrust);
                    break;
            }
        }

        #endregion

        #region Create Missiles Functions

        public void CreateMissiles(ICalculatedFighter CalculatedFighter)
        {
            createMissiles(CalculatedFighter);
        }

        private void createMissiles(ICalculatedFighter calculatedFighter)
        {
            int missilesSum = 0;
            int missilesValue = 0;
            foreach (Fighter fighter in this)
            {
                missilesSum += fighter.Missiles;
            }

            if (missilesSum >= 0 && missilesSum <= 10) missilesValue = 0;
            if (missilesSum >= 11 && missilesSum <= 25) missilesValue = 10;
            if (missilesSum >= 26 && missilesSum <= 50) missilesValue = 25;
            if (missilesSum >= 51 && missilesSum <= 75) missilesValue = 50;
            if (missilesSum >= 76 && missilesSum <= 100) missilesValue = 75;
            if (missilesSum > 100) missilesValue = 100;

            calculatedFighter.setMissiles(missilesValue);
        }

        #endregion
    }
}
